function merge(arr1, arr2) {
    let index1 = 0, index2 = 0;
    let arr = [];
    if (!arr1.length || !arr2.length) {
        arr = arr1 ? arr1 : (arr2 || []);
        return arr;
    }
    while (index1 < arr1.length || index2 < arr2.length) {
        if (arr1[index1] < arr2[index2]) {
            arr.push(arr1[index1]);
            index1++;

        } else if (arr1[index1] > arr2[index2]) {
            arr.push(arr2[index2]);
            index2++;
        } else {
            arr.push(arr1[index1]);
            arr.push(arr2[index2]);
            index1++;
            index2++;
        }
        if (index1 < arr1.length && index2 >= arr2.length) {
            arr.push(...arr1.slice(index1));
            return arr;
        }
        if (index2 < arr2.length && index1 >= arr1.length) {
            arr.push(...arr2.slice(index2));
            return arr;
        }
    }
    return arr;
}
console.log(merge([1, 2, 3, 4], [5, 6]))